﻿@typeparam TData

<Space Direction="@DirectionVHType.Vertical" Size="@("small")">
    @if (Query != null)
    {
        <SpaceItem>
            <Space Size=@(("8", "8")) Wrap>
                <SpaceItem>
                    <Select Mode="multiple"
                            Placeholder="选择查询的字段"
                            @bind-Values="@_selectedValues"
                            TItemValue="string"
                            @ref="Select"
                            TItem="string"
                            Style="width: 200px;"
                            EnableSearch
                            OnSelectedItemsChanged="OnSelectedItemsChanged"
                            AllowClear>
                        <SelectOptions>
                            @if (ViewModelFields != null)
                            {
                                @foreach (var item in ViewModelFields)
                                {
                                    if (string.IsNullOrEmpty(item.DisplayName)) continue;
                                    if (!item.IsPanel) continue;
                                    <SelectOption TItemValue="string" TItem="string" Value=@item.TypeName Label=@item.DisplayName />
                                }
                            }
                        </SelectOptions>
                    </Select>
                </SpaceItem>
                @if (Query.QueryData.Count > 0)
                {
                    @foreach (var item in Query.QueryData)
                    {
                        if (item.Value == HideQuery) continue;
                        var key = item.Key;
                        var field = ViewModelFields.SingleOrDefault(u => u.TypeName == key);
                        var tableItem = GetQueryItems?.Invoke(key);
                        if (tableItem != null)
                        {
                            <SpaceItem>
                                @tableItem
                            </SpaceItem>
                            continue;
                        }
                        <SpaceItem>
                            @if (field.IsEnum)
                            {
                                <Select TItem="string"
                                        TItemValue="string"
                                        @bind-Value="@Query.QueryData[key]"
                                        Placeholder="@($"{field.DisplayName}")"
                                        Style="width:200px">
                                    <SelectOptions>
                                        @foreach (var item in field.EnumValueName)
                                        {
                                            <SelectOption TItemValue="string" TItem="string" Value="item.Key.ToString()" Label="@item.Value" />
                                        }
                                    </SelectOptions>
                                </Select>
                            }
                            else
                            {
                                <Input Placeholder="@($"{field.DisplayName}搜索")" @bind-Value="@Query.QueryData[key]" />
                            }

                        </SpaceItem>
                    }
                }
                <SpaceItem>
                    <RangePicker TValue="DateTime?[]" ShowTime='@("HH:mm")' OnChange="OnRangeChange" />
                </SpaceItem>
                <SpaceItem>
                    <Button Type="@ButtonType.Primary" OnClick="FuzzyQuery">查询</Button>
                </SpaceItem>
            </Space>
        </SpaceItem>
    }
    <SpaceItem>

        <Space>
            @if (Buttons != null)
            {
                foreach (var item in Buttons)
                {
                    if (item.ButtonPosition == ButtonPosition.Default && item.MenuType == MenuType.Button)
                    {
                        <SpaceItem>
                            @CreatePackButton(item, default)
                        </SpaceItem>
                    }
                }
            }
        </Space>

    </SpaceItem>
</Space>


<Table DataSource="DataSource"
       @ref="Table"
       ScrollX="1300"
       Bordered="true"
       HidePagination
       PageSize="PageSize"
       TItem="TData"
       Loading="Loading"
       TreeChildren="TreeChildren">

    @if (ViewModelFields != null && ViewModelFields.Count > 0)
    {
        if (HasTableSelection)
        {
            <Selection CheckStrictly />
        }

        foreach (var item in ViewModelFields)
        {

            if (!item.IsPanel) continue;
            item.Value = context;
            var tableItem = GetTableItems?.Invoke(item);
            if (tableItem != null)
            {
                @tableItem
                continue;
            }
            var typeNmae = item.ModelType.ToLower();
            if (item.IsEnum)
            {
                <Column Title="@item.DisplayName" TData="string" Width="@item.Width">
                    @{
                        var obj = context.GetObjValue(item.TypeName);
                        if (obj != null)
                        {
                            var value = (int)obj;
                            @item.EnumValueName[value]
                        }
                    }
                </Column>
                continue;
            }
            switch (typeNmae)
            {
                case "string":
                    <Column Title="@item.DisplayName" DataIndex="@item.TypeName" TData="string" Width="@item.Width" />
                    break;
                case "int32":
                    <Column Title="@item.DisplayName" DataIndex="@item.TypeName" TData="int?" />
                    break;
                case "boolean":
                    <Column Title="@item.DisplayName" DataIndex="@item.TypeName" TData="bool?">
                        @{
                            var obj = context.GetObjValue(item.TypeName);
                            if (obj != null)
                            {
                                var value = (bool)obj;
                                if (value)
                                {
                                    <Tag Color="green">是</Tag>
                                }
                                else
                                {
                                    <Tag Color="pink">否</Tag>
                                }
                            }
                        }
                    </Column>
                    break;
                case "guid":
                    <Column Title="@item.DisplayName" DataIndex="@item.TypeName" TData="Guid?" />
                    break;
                case "datetime":
                    <Column Title="@item.DisplayName" DataIndex="@item.TypeName" TData="DateTime?" />
                    break;
                case "double":
                    <Column Title="@item.DisplayName" DataIndex="@item.TypeName" TData="double?">
                    </Column>
                    break;
                case "float":
                    <Column Title="@item.DisplayName" DataIndex="@item.TypeName" TData="float?" />
                    break;
                case "short":
                    <Column Title="@item.DisplayName" DataIndex="@item.TypeName" TData="short?" />
                    break;
                case "byte":
                    <Column Title="@item.DisplayName" DataIndex="@item.TypeName" TData="byte?" />
                    break;
                case "icon"://特殊字段编码
                    <Column Title="@item.DisplayName" DataIndex="@item.TypeName" TData="string">
                        @{
                            var obj = context.GetObjValue(item.TypeName);
                            if (obj != null)
                            {
                                <Icon Type="@obj.ToString()" Theme="outline" />
                            }
                        }
                    </Column>
                    break;
                case "headportrait":
                    <Column Title="@item.DisplayName" DataIndex="@item.TypeName" TData="string">
                        @{
                            var obj = context.GetObjValue(item.TypeName);
                            if (obj != null)
                            {
                                <Avatar Src="@obj.ToString()" />
                            }
                            else
                            {
                                <Avatar Style="background-color: #87d068" Icon="user" />
                            }
                        }
                    </Column>
                    break;
                default:
                    break;
            }

        }
        if (Buttons != null && Buttons.Count > 0)
        {
            List<ViewMenu> but = Buttons.Where(u => u.ButtonPosition == ButtonPosition.Row && u.MenuType == MenuType.Button).ToList();
            if (but.Count > 0)
            {
                <ActionColumn Title="操作">
                    @if (but.Count >= 3)
                    {
                        <Dropdown>
                            <Overlay>
                                <Menu>
                                    @foreach (var item in but)
                                    {
                                        <MenuItem>
                                            @CreatePackButton(item, context)
                                        </MenuItem>
                                    }

                                </Menu>
                            </Overlay>
                            <ChildContent>
                                <a class="ant-dropdown-link" @onclick:preventDefault>
                                    更多 <Icon Type="down" />
                                </a>
                            </ChildContent>
                        </Dropdown>

                    }
                    else
                    {
                        <Space>
                            @foreach (var item in but)
                            {
                                <SpaceItem>
                                    @CreatePackButton(item, context)
                                </SpaceItem>
                            }
                        </Space>


                    }
                </ActionColumn>
            }
        }
    }
</Table>
<div style="float:right;margin-top:20px">
    <Pagination Total="Total"
                @ref="Pagination"
                ShowTotal=showTotal
                PageSize="PageSize"
                Current="PageIndex"
                OnChange="PageIndexChanged"
                OnShowSizeChange="PageIndexChanged" />
</div>


@code{
    RenderFragment CreatePackButton(ViewMenu menu, TData context)
    {
        RenderFragment render = null;
        if (menu.IsDoubleTrue)
        {
            string title = $"确认{menu.MenuName}该数据吗";
            render =@<Popconfirm Title='@title'
                                 OkText='确认'
                                 CancelText="取消"
                                 OnConfirm="()=>ButtonClick(menu,context)">
        @CreateButton(menu, context)
    </Popconfirm>;
}
else
{
render = CreateButton(menu, context);
}
return render;
}

RenderFragment CreateButton(ViewMenu menu, TData context)
{

RenderFragment render = null;
string type = menu.ButtonPosition == ButtonPosition.Default ? "primary" : "link";
bool danger = menu.IsDoubleTrue == true ? true : false;
render =@<Button Icon="@menu.Icon" Type="@type" Danger="danger" Style="margin-bottom:16px" OnClick="() => doubleTrueClick(menu, context)">
    @if (menu.TargetType == TargetType.NewLabel)
    {
        <a href="@menu.Url" target="_blank">@menu.MenuName</a>
    }
    else
    {
        @menu.MenuName
    }
</Button>;
return render;
}

void doubleTrueClick(ViewMenu menu, TData context)
{
if (menu.IsDoubleTrue) return;
ButtonClick(menu, context);
}

Func<PaginationTotalContext, string> showTotal = ctx => $"{ctx.Range.Item1}-{ctx.Range.Item2} 共 {ctx.Total} 条";
}